/*
 * A simple ToDo Manager
 *
 * Copyright (c) 2011 Nosheen Zaza, Manuel Rebollo, Seyedsaeid Mahpeikar and
 * Wo Ke
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package todomanager.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.font.TextAttribute;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.FontUIResource;
import javax.swing.plaf.metal.DefaultMetalTheme;
import javax.swing.plaf.metal.MetalLookAndFeel;

/**
 * Sets the theme of the application. Including all the fonts and colors used.
 *
 * @author
 * Seyedsaeid Mahpeikar
 * @author
 * Nosheen Zaza
 */
public class ToDoTheme {
    private static ResourceBundle themeBundle = ResourceBundle.getBundle(
                "todomanager/ui/ThemeBundle");
    private static class ColorTheme extends DefaultMetalTheme {

        /* Extract color codes from Theme resource bundle. */
       
        private final ColorUIResource primary1 = new ColorUIResource(
                Integer.valueOf(themeBundle.getString("primary1Red")),
                Integer.valueOf(themeBundle.getString("primary1Green")),
                Integer.valueOf(themeBundle.getString("primary1Blue")));
        private final ColorUIResource primary2 = new ColorUIResource(
                Integer.valueOf(themeBundle.getString("primary2Red")),
                Integer.valueOf(themeBundle.getString("primary2Green")),
                Integer.valueOf(themeBundle.getString("primary2Blue")));
        private final ColorUIResource primary3 = new ColorUIResource(
                Integer.valueOf(themeBundle.getString("primary3Red")),
                Integer.valueOf(themeBundle.getString("primary3Green")),
                Integer.valueOf(themeBundle.getString("primary3Blue")));
        private final ColorUIResource secondary1 = new ColorUIResource(
                Integer.valueOf(themeBundle.getString("secondary1Red")),
                Integer.valueOf(themeBundle.getString("secondary1Green")),
                Integer.valueOf(themeBundle.getString("secondary1Blue")));
        private final ColorUIResource secondary2 = new ColorUIResource(
                Integer.valueOf(themeBundle.getString("secondary2Red")),
                Integer.valueOf(themeBundle.getString("secondary2Green")),
                Integer.valueOf(themeBundle.getString("secondary2Blue")));
        private final ColorUIResource secondary3 = new ColorUIResource(
                Integer.valueOf(themeBundle.getString("secondary3Red")),
                Integer.valueOf(themeBundle.getString("secondary3Green")),
                Integer.valueOf(themeBundle.getString("secondary3Blue")));

        @Override
        protected ColorUIResource getPrimary1() {
            return primary1;
        }

        @Override
        protected ColorUIResource getPrimary2() {
            return primary2;
        }

        @Override
        protected ColorUIResource getPrimary3() {
            return primary3;
        }

        @Override
        protected ColorUIResource getSecondary1() {
            return secondary1;
        }

        @Override
        protected ColorUIResource getSecondary2() {
            return secondary2;
        }

        @Override
        protected ColorUIResource getSecondary3() {
            return secondary3;
        }

        @Override
        public FontUIResource getMenuTextFont() {
            return new FontUIResource("Dialog", Font.PLAIN, 12);
        }

        @Override
        public FontUIResource getControlTextFont() {
            return new FontUIResource("Dialog", Font.BOLD, 11);
        }
    }

    /*
     * Apply ToDo theme.
     */
    public static void applyToDoTheme() {
        Font fontNormal = new Font("Dialog", Font.PLAIN, 12);

        Map attributes = fontNormal.getAttributes();

        attributes.put(TextAttribute.STRIKETHROUGH,
                TextAttribute.STRIKETHROUGH_ON);
        
        Font strikedFont = new Font(attributes);

        MetalLookAndFeel.setCurrentTheme(new ColorTheme());
        // We want our table headers to have no borders and be transparent.
        UIManager.getDefaults().put(
                "TableHeader.cellBorder", BorderFactory.createEmptyBorder());
        UIManager.getDefaults().put(
                "TableHeader.background", new Color(255, 255, 255));
        UIManager.getDefaults().put(
                "Font.Dialog-Bold-10", new Font("Dialog", Font.BOLD, 10));
        UIManager.getDefaults().put(
                "Font.Dialog-Plain-10", new Font("Dialog", Font.PLAIN, 10));
        UIManager.getDefaults().put(
                "Font.Dialog-Plain-12", fontNormal);
        UIManager.getDefaults().put(
                "Font.Dialog-Plain-Striked-12", strikedFont);
        UIManager.getDefaults().put("Color.Gray", Color.GRAY);
        UIManager.getDefaults().put("Color.Black", Color.BLACK);
        UIManager.getDefaults().put("Color.Red", Color.RED);
        UIManager.getDefaults().put("Color.Selected-cell", Color.ORANGE);
        UIManager.getDefaults().put("Color.Table-Grid", new Color(
                Integer.parseInt(themeBundle.getString("Transparent.All")),
                Integer.parseInt(themeBundle.getString("Transparent.All")),
                Integer.parseInt(themeBundle.getString("Transparent.All")),
                Integer.parseInt(themeBundle.getString("Transparent.All"))));

        /*
         * Set metal look and feel. This is needed because on some platforms
         * (like Mac OS) it is not the default look and feel.
         */
        try {
            UIManager.setLookAndFeel(new MetalLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(ToDoTheme.class.getName()).log(Level.ALL, null, ex);
        }
    }
}
